Learning Petit Lesson 1: Getting Started and the Console
Welcome to my series of tutorials. My goal is simple: to provide clear, concise, and professional tutorials. I will cover topics ranging from absolute-novice level to advanced. If you want to skip the paragraphs of exposition and rhetorical questions and get straight to learning, you should follow my tutorials. This introductory tutorial is intended for absolute beginners with no programming experience - programmers with any experience will probably want to skip it (although I recommend checking the keyboard shortcuts for some 'secret' ones). After explaining the basics of the Petit Computer interface, I will introduce you to fundamental programming concepts and then explain how they relate to Petit Computer. Please do not be daunted by the length; much of the information in this lesson is for reference, and while it would certainly benefit you to learn the terms and shortcuts, don't feel pressured to memorize them. You can always refer back to this lesson later. You can navigate between the lessons using links at the start and end of each lesson, or by visiting the Learning Petit category page 'The interface' When you first launch Petit Computer you will be taken to the home menu, which has "Write Program", "View Gallery", and "File Management". Since these tutorials focus on programming, you will want to launch "Write Program". 'Run Mode' This will take you to a black screen with white text at the top; this is called a console (or terminal), a place where you interact with the computer through text. This is referred to as "Run Mode" in the software. Run Mode is a good place to start learning, because when no program is running you can type in a command and when you press enter it will execute the command immediately. We will get to this soon. On the bottom screen you can see they keyboard panel and some other buttons. The five black function keys along the top, although they look like menu buttons, actually just paste a command where you are typing. The X in the upper right returns you to the home menu. Along the bottom you will see the Help, RUN, and EDIT keys. The Help key takes you into the Help screen (feel free to check it out, but it is poorly translated from Japanese, extremely technical, and will be almost useless to anyone who has never programmed before) The RUN key takes you to the Run Mode screen, where you are now. The EDIT key takes you to the code editing window, where you can write a program. Switch to EDIT Mode. 'Edit Mode' Edit mode is straightforward. On the upper screen you can see a series of numbered arrows. These numbers are line numbers; each line can contain some code. When you type, the text you are typing will appear where the cursor is located on the current line. Later on I will go over keyboard shortcuts, including some that aren't listed in the Help! Return to Run Mode 'Getting started programming' Almost every beginning programming tutorial will have you start with "Hello, World", so let's begin there. Type PRINT "HELLO, WORLD" into the console and press enter. :HELLO, WORLD :OK As you can see, the text you entered was repeated on the console. PRINT is a command, which means it tells Petit Computer to do something - in this case, display text on the screen. "HELLO, WORLD" is a string, which is a written message. Let's look at a few more lines of code (note I have bolded what you type to distinguish it from what the computer says afterward): :PRINT 55 :55 :OK :X = 200 :OK :PRINT X :200 :OK Here you see some different things we can print to the console. If you've ever taken algebra, you probably understand the code above, but let's cover some definitions. 'Definitions' Here we will cover some essential definitions for fundamental programming concepts. These are extremely important to learn, but don't feel overwhelmed to understand and memorize everything at once. This will make more sense later on and you can always refer back to it later. In the above code, PRINT is a command, "HELLO, WORLD" is a string, 55 is a value, and X is a variable. *'Command': Instructs the computer to perform an action, such as display text on the screen *'Value': A value is a number, or other type of information, we can use in operations or store. Values can be: **Numbers (either whole numbers, called integers, or decimal numbers) **'Strings': A string is a sequence of characters between quotes, usually used to store and display messages or names *'Variable': A place in memory where we can store values. You can choose your own names for variables (provided they aren't reserved words, like PRINT). Variables that store strings must end in a dollar sign, like NAME$. A variable exists as soon as you use its name; if you haven't given it a value before it will have a value of 0'. ''You can use a variable anywhere you can use a value, since a variable contains a value, which is why we can say PRINT 55 or PRINT X. Variables are not stored permanently; they will be lost if you exit Petit Computer or run the CLEAR command *'''Argument: An argument is a value that you give to a command. When you say PRINT 55, you are using 55 as the argument. Most commands require one or more arguments. Again, you can use a value or variable in an argument, since a variable represents a value. Be careful not to confuse strings and variables. If you enter PRINT "HELLO", the output will be HELLO, but if you enter PRINT HELLO, the output will be 0 (or whatever value is currently stored in the variable HELLO). :Tip: Give important variables descriptive names. Unimportant or temporary variables can have short names like X as long as the usage is clear. 'Practical examples' Here we will write a simple application to enhance your understanding of those definitions, and introduce the new command INPUT. Switch to Edit Mode and enter the following program: : :INPUT "Enter your name"; NAME$ :PRINT "Hello, " NAME$ :INPUT "Enter your age"; AGE :PRINT NAME$ " is " AGE :DBLAGE = AGE * 2 :PRINT "Twice your age is " DBLAGE Go back to Run Mode and enter RUN (you may wish to use the 5th function key shortcut at the top of the touchscreen, but you still have to press enter). When you see a blinking cursor, type in an answer (I have italicized sample answers here): :Enter your name? :Kevin :Enter your age? :100 :Kevin is 100 :Twice your age is 200 Here we asked the user to enter their name, greeted them by name, asked them to enter their age, stated their age, and then doubled their age and displayed that. Several new ideas are introduced: *The INPUT command displays message (with a ? added) and prompts the user to enter a value which is then stored into variable (see Notes below for more information on INPUT) *We can store the result of a mathematical expression into a variable (in this case, DBLAGE) *We can give multiple values to PRINT to display together, allowing us to combine strings and variable values in our output That's enough to get you started with console applications. You should play around with what you've learned, or attempt some of the Recommended Exercises below. In the next lesson, we will go over some ways to control the flow of your application and respond to conditions. 'Other useful commands' *CLS: Clears all text from the screen *CLEAR: Resets all variables (this doesn't erase any code you have entered in Edit Mode) :TIP: Put CLS and CLEAR at the top of every program you write Saving/loading your work You can save your program (what you have typed into Edit Mode) by switching to Run Mode and entering SAVE "filename" and load it at any time by entering LOAD "filename", where filename is a name of your choice :Tip: Petit has no undo tool, so it's a good idea to save temporary copies of the program you are working on alongside the primary copy. For example, if I'm writing a game called Shooter, I will save my work to ShooterTemp every 5 minutes or so and save to Shooter when I finish an important feature or I'm done for the day. That way if I accidentally delete some important code, I can re-load the temp file, and if I accidentally save a bad version of the program, I have a backup copy Keyboard shortcuts Shortcuts you will probably find very handy in Edit Mode: *'Y': Backspace *'A': Enter *'L/R': Shift (you can use L or R interchangeably but I will usually write L for convenience) Some more-or-less secret shortcuts (these are very useful): *'L+Y': Delete whole line *'L+Left': Jump to beginning of line *'L+Right': Jump to end of line *'L+Up': Scroll up one page (24 lines) *'L+Down': Scroll down one page (24 lines) Also note the 4 icons in the bottom right corner of the touchscreen; normally you see 4 arrows, but if you tap the tiny down arrow to the left of them, you will find the copy and paste buttons. The copy button copies the entire line you are currently on, while paste puts the copied text where the cursor is. You can paste the same text repeatedly. :Tip: If you are going to be using a variable name often and don't want to keep typing it in, enter it on its own line and copy that line. Then you can paste the variable name into your code where needed :Tip: You can use copy and paste to move small amounts of code, but if you want to completely rearrange your code, you need to connect your SD card to a computer and make the changes in a word processor :Tip: Some people will find it maddening to type out entire programs on the little DS touchscreen keyboard, but I actually like writing code on my DS; since I can't write as fast I have more time to think and I actually '''make fewer mistakes' than I would on the computer. Sometimes slow and steady is the way to go, and code that is well thought-out is always better than code that was typed as fast as you could think.'' Recommended exercises At the end of each lesson I will suggest some simple programs you can write to practice what you've learned. *'Mad Libs': Write a program that asks the user to enter several things (for example, a name, a tool, and a number) and then displays a funny story using those values ("My sister FRED got stuck in an elevator with a GARDEN GNOME and now she's pregnant with 175 babies!"). Force your friends to try it out *'Calculator:' Write a program that asks the user for some numbers and then does some math with them and displays the result; for example, ask for the user's weight and then print out what they would weigh on the moon or on Jupiter Notes Some lessons may end with a Notes section where I point out interesting additional details about subjects brought up in the lesson. They may sometimes be confusing to learners, so feel free to ignore them *INPUT is possibly the single most bizarre command in all of Petit. As far as I know, it is the only command that uses a semicolon. The string argument is optional (e.g. you can write INPUT NAME$ and the prompt will just be '?'); with most or all other commands optional arguments come last and are separated by commas, not semicolons. It is also unusual for a command to store a value into a variable, usually this is done by functions, which will be described later. Input would make much more sense as a function, but perhaps they couldn't figure out a way to have a function with an optional argument Category:Tutorials Category:Programming Concepts Category:Resources Category:Kevin's Learning Petit Lessons